package mall.promotion.impl;

import mall.common.enums.PromotionTypeEnum;
import mall.pojo.ProductDO;
import mall.promotion.PromotionBase;
import mall.promotion.PromotionStrategy;

import java.util.List;
import java.util.Set;

/**
 * 满减活动 策略的实现类
 * @author zmy
 */
public class FullReductionPromotion extends PromotionBase implements PromotionStrategy {
    /** 满减的阈值条件 */
    private double threshold;
    /** 减少的值 */
    private double reduction;

    public FullReductionPromotion() {}

    public FullReductionPromotion(String promotionId, String name, String promotionType, boolean enabled, int priority, double threshold, double reduction) {
        super(promotionId, name, promotionType, enabled, priority);
        this.threshold = threshold;
        this.reduction = reduction;
    }

    public FullReductionPromotion(String promotionId, String name, String promotionType, Set<String> productIds, boolean enabled, int priority, double threshold, double reduction) {
        super(promotionId, name, promotionType, productIds, enabled, priority);
        this.threshold = threshold;
        this.reduction = reduction;
    }

    @Override
    public String getId() {return getPromotionId();}


    @Override
    public boolean isEnabled() {return getEnabled();}


    @Override
    public boolean isApplicable(List<ProductDO> shopCar) {
        // 判断是否达到满减条件
        if (getPromotionType().equals(PromotionTypeEnum.ALL.type)) {    // 全场满减直接所有都减
            return shopCar.stream()
                    .mapToDouble(p -> p.getPrice() * p.getQuantity())
                    .sum()
                    >= threshold;
        } else {    // 单个商品满减
            return shopCar.stream()
                    .filter(p -> getProductIds().contains(p))
                    .mapToDouble(p -> p.getPrice() * p.getQuantity())
                    .sum()
                    >= threshold;
        }
    }

    @Override
    public double apply(List<ProductDO> shopCar) {
        return isApplicable(shopCar) ? reduction : 0;
    }

    @Override
    public String toString() {
        return "FullReductionPromotion{" +
                super.toString() +
                "threshold=" + threshold +
                ", reduction=" + reduction +
                "} ";
    }
}
